package cn.est.serviceImpl;

import cn.est.dto.MalFunctionDto;
import cn.est.dto.MalfunctionOptionsDto;
import cn.est.dto.Page;
import cn.est.mapper.MalfunctionMapper;
import cn.est.mapper.MalfunctionOptionsMapper;
import cn.est.pojo.Malfunction;
import cn.est.service.MalfunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MalfunctionServiceImpl implements MalfunctionService {

    @Autowired
    private MalfunctionMapper malfunctionMapper;

    @Autowired
    private MalfunctionOptionsMapper malfunctionOptionsMapper;

    public Malfunction getMalfunctionById(Long id) {
        return malfunctionMapper.getMalfunctionById(id);
    }

    public List<Malfunction> getMalfunctionListByMap(Map<String, Object> param) {
        return malfunctionMapper.getMalfunctionListByMap(param);
    }

    public Integer getMalfunctionCountByMap(Map<String, Object> param) {
        return malfunctionMapper.getMalfunctionCountByMap(param);
    }

    public Integer qdtxAddMalfunction(Malfunction malfunction) {
        malfunction.setCreatdTime(new Date());
        return malfunctionMapper.insertMalfunction(malfunction);
    }

    public Integer qdtxModifyMalfunction(Malfunction malfunction) {
        malfunction.setUpdatedTime(new Date());
        return malfunctionMapper.updateMalfunction(malfunction);
    }

    public Integer qdtxDeleteMalfunctionById(Long id) {
        return malfunctionMapper.deleteMalfunctionById(id);
    }

    public Integer qdtxBatchDeleteMalfunction(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return malfunctionMapper.batchDeleteMalfunction(param);
    }

    public Page<Malfunction> queryMalfunctionPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = malfunctionMapper.getMalfunctionCountByMap(param);
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Malfunction> malfunctionList = malfunctionMapper.getMalfunctionListByMap(param);
        page.setList(malfunctionList);
        return page;
    }

    @Override
    public List<MalFunctionDto> getMalFunctionDto(Integer modelId) {
        //1.获取所有的MalFunctionDto
        List<MalFunctionDto>  malFunctionDtoList =  malfunctionMapper.selectListByModelId(modelId);
        if (malFunctionDtoList!=null && !malFunctionDtoList.isEmpty()){
            for (MalFunctionDto malfuncitondto: malFunctionDtoList) {
                Long mid =malfuncitondto.getId();
                if(mid!=null){
                    //  2.根据MalFunctionDto 里面的id获取子分类---》malfunctionOption
                }
                      List<MalfunctionOptionsDto> optionsDtoList=malfunctionOptionsMapper.selectMalFuncionOptions(mid);
                //3.封装数据返回
                      malfuncitondto.setMalfunctionOptionsDtosList(optionsDtoList);
                }
        }
        return malFunctionDtoList;
    }
}
